//
//  YLSearchDetailViewController.m
//  Style
//
//  Created by YL on 15/10/6.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "YLSearchDetailViewController.h"
#import "AFNetWorkingW.h"
#import <MJRefresh.h>
#import "YLSearchModel.h"
#import "YLSearchDetailTableViewCell.h"
#import "YLSearchWebViewController.h"
#import "YLDetail_model.h"

@interface YLSearchDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YLSearchModel *searchModel;
@property (nonatomic, assign) NSInteger nextPage;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSMutableDictionary *mDic;
@property (nonatomic, retain) NSMutableArray *mArr;
@property (assign, nonatomic) BOOL automaticallyHidden;
@property (nonatomic, assign) BOOL open;
@property (nonatomic, retain) UIActivityIndicatorView *activity;
@end

@implementation YLSearchDetailViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"文章";
    self.mArr = [NSMutableArray array];
    self.start = 0;
    [self dataHandle];
    [self tableViewsubViews];
    [self setupRefresh];
    self.activity = [[UIActivityIndicatorView alloc] init];
    self.activity.color = [UIColor blackColor];
    self.activity.frame = CGRectMake(0, 0, 50, 50);
    self.activity.center = self.view.center;
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    [self setExtraCellLineHidden:self.tableView];
}

/* 数据处理 */
- (void)dataHandle
{
    self.url = [NSString stringWithFormat: @"auth=&client=1&deviceid=BA920378-52B5-43F2-BE1A-5404120EDD7A&keyword=%@&limit=10&start=%ld&type=content&version=3.0.6", self.searchText, (long)self.start];
    [AFNetWorkingW netWorkWithURL:@"http://api2.pianke.me/search/get" body:self.url resultBlock:^(id result) {
        self.mDic = result;
        if ([[[self.mDic objectForKey:@"data"] objectForKey:@"list"] count] == 0 && self.start == 10){
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"搜索结果为空" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self.activity stopAnimating];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:^{
                
            }];
        }
        
        NSDictionary *dic = [self.mDic objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"list"];
        
        if ([arr count] == 0) {
            [self.tableView.footer noticeNoMoreData];
        } else
        {
            for (NSDictionary *dic1 in arr) {
                self.searchModel = [[YLSearchModel alloc] init];
                [self.searchModel setValuesForKeysWithDictionary:dic1];
                [self.searchModel setIcon:[[dic1 objectForKey:@"userinfo"] objectForKey:@"icon"]];
                [self.mArr addObject:self.searchModel];
                
                [self.tableView.footer endRefreshing];
            }
            [self.activity stopAnimating];
            [self.tableView reloadData];
        }
    }];
    self.start = self.start + 10;
}

#pragma mark ** tableView相关设置
/* tableView布局 */
- (void)tableViewsubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth(self.view), ViewHeight(self.view)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[YLSearchDetailTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

/* 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.mArr count];
}

/* cell设置 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLSearchDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    self.searchModel = [self.mArr objectAtIndex:indexPath.row];
    cell.searchModel = self.searchModel;
    return cell;
}

/* 行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ViewHeight(self.view) / 3.5;
}

/* cell选择 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLSearchWebViewController *head = [[YLSearchWebViewController alloc] init];
//    head.detail_model = [self.mArr objectAtIndex:indexPath.row];
    head.searchModel = [self.mArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:head animated:YES];
}

/* 分割线隐藏 */
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}


#pragma mark ** 刷新
- (void)setupRefresh
{
    /* 上拉刷新 */
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    [self.tableView.header beginRefreshing];
    /* 下拉加载数据 */
    
    self.tableView.footer = [MJRefreshAutoGifFooter  footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    [self.tableView.footer beginRefreshing];
}

- (void)headerRefreshing
{
    [self.tableView reloadData];
    //    [self dataHandle];
    [self.tableView.header endRefreshing];
}

- (void)footerRefreshing
{
    self.tableView.footer.automaticallyHidden = YES;
    [self dataHandle];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
